//
//  PJTestViewController.m
//  PeiJue
//
//  Created by lxy on 2017/9/21.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "PJTestViewController.h"
#import "PJOneViewController.h"
#import "PJTwoViewController.h"
#import "ThreeViewController.h"
#import "XYUserHelper.h"


@interface PJTestViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSMutableArray *dataList;

@end

@implementation PJTestViewController
@synthesize tableView = _tableView;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *rightBt = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtAction:)];
    self.navigationItem.rightBarButtonItem = rightBt;
    
    /*
    [LCActionSheet xy_configureActionSheet];
    [XYUtils showMessageWithTitle:nil message:@"Dispose of any resources that can be recreated" confirmTitle:@"xxxx" confirmHandler:^(UIAlertAction *action) {
        [[XYUserHelper shareInstance] noticeNeedAuthByRequestTokenInvalid];
        LCActionSheet *sheet = [LCActionSheet sheetWithTitle:@"提示" cancelButtonTitle:@"取消" clicked:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
            if (buttonIndex == 0) {
                [self.navigationController pushViewController:[[PJOneViewController alloc] init] animated:YES];
            }else if (buttonIndex == 2) {
                NSLog(@"2");
            }
        } otherButtonTitles:@"标题01",@"标题02", nil];
        [sheet show];
    }];
    */
    
    
    // 第一次进页面请求数据，设置loading, 暂不设置下拉刷新
    [XYEmptyViewFactory xy_emptyMianView:self.tableView image:[UIImage imageWithGIFNamed:@"loading.gif"] title:@"正在加载中...." subtitle:nil];
    self.hasRefresh = NO;
    [self loadNewData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Events
- (void)rightBtAction:(UIBarButtonItem *)item{
    [self.dataList removeAllObjects];
    self.hasLoadMore = NO;
    [self updateUI];
}

#pragma mark - Overrides
- (void)loadNewData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.dataList = @[@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1"].mutableCopy;
        self.hasRefresh = YES;
        [self endRefreshing];

        //根据条件判断是否需要显示加载更多
        self.hasLoadMore = YES;
        [self updateUI];
    });
}

- (void)loadMoreData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableArray *array = @[@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1"].mutableCopy;
        [_dataList addObjectsFromArray:array];
        
        [self endRefreshing];
        [self noticeNoMoreData];
        [self updateUI];
    });
}

- (void)updateUI {
    [XYEmptyViewFactory xy_emptyMianView:self.tableView image:kDefImageNoData title:@"请求失败" subtitle:@"根据条件判断是否需要显示加载更多"];
    [self.tableView reloadData];

}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *s = self.dataList[indexPath.row];
    if ([s isEqualToString:@"2"]) {
        return 300;
    }
    return 60;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.textLabel.text = @"少时诵诗书";
   
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.navigationController pushViewController:[[PJOneViewController alloc] init] animated:YES];
}

#pragma mark - Getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[TableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor whiteColor];
        if (@available(iOS 11.0, *)) {
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.contentInset = UIEdgeInsetsMake(KNavBarHeight, 0, KTabBarHeight, 0);
        _tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
       
    }
    return _tableView;
}

@end
